import 'dart:async';

import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/dialog/input_output_dialog.dart';
import 'package:full_display/page/montage/dialog/m_angle_setting_dialog.dart';
import 'package:full_display/page/montage/dialog/m_bluetooth_setting_dialog.dart';
import 'package:full_display/page/montage/dialog/m_clock_dialog.dart';
import 'package:full_display/page/montage/dialog/m_pinjie_dialog.dart';
import 'package:full_display/page/montage/dialog/m_pinjie_manager_dialog.dart';
import 'package:full_display/page/montage/dialog/wifi_setting_dialog.dart';
import 'package:full_display/page/montage/drawer/montage_drawer_widget.dart';
import 'package:full_display/page/montage/widgets/montage_video_list.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_util.dart';
import 'package:full_display/unit/screen/screen_util1.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/socket/udp/socket_udp_utils.dart';
import 'package:full_display/unit/widgets/drawer/drawer_widget.dart';
import 'package:full_display/unit/widgets/widget_dialog/widget_dialog.dart';
import 'package:full_display/unit/widgets/widget_media_list.dart';
import 'package:provider/provider.dart';
import 'montage_provider.dart';

final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();

class MontageWidget extends StatelessWidget {
  const MontageWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: const MontagePage(),
    );
  }
}

class MontagePage extends StatefulWidget {
  const MontagePage({super.key});

  @override
  State<MontagePage> createState() => _MontagePageState();
}

class _MontagePageState extends State<MontagePage> {
  var _montageProvider = MontageProvider();
  TextEditingController nameController = TextEditingController();

  @override
  void initState() {
    super.initState();
    //每个10秒执行一次任务
    // Timer.periodic(const Duration(seconds: 10), (timer) {
    //   SocketUDPUtils().sendUDPMsg("SSDP:BROADCAST2");
    // });
    // SocketUDPUtils().waitUDPMsg();
  }

  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Scaffold(
      backgroundColor: ColorPlate.background,
      resizeToAvoidBottomInset: false,
      drawer: MontageDrawerWidget(),
      // onDrawerChanged: (isOpened) {
      //   if (isOpened == false) {}
      // },
      key: _scaffoldKey,
      appBar: AppBar(
        backgroundColor: ColorPlate.background,
        elevation: 0,
        leading: IconButton(
          icon: Image.asset(
              height: 20.0.H,
              width: 20.0.W,
              AssetUtils.getAsset2Image("icon_fold")),
          onPressed: () {
            _scaffoldKey.currentState?.openDrawer();
          },
        ),
        actions: [
          iconTextBtn(0, "WiFi设置", "icon_wifi", btnClick),
          iconTextBtn(1, "蓝牙设置", "icon_bluetooth", btnClick),
          iconTextBtn(2, "角度调节", "angle_w", btnClick),
          iconTextBtn(3, "拼接管理", "splic_manager", btnClick),
          iconTextBtn(4, "定时", "clock", btnClick),
          iconTextBtn(5, "添加拼接", "icon_add", btnClick),
        ],
      ),
      body: SingleChildScrollView(
        child: Stack(
          children: [
            Container(
              height: ScreenUtil1.screenHeight -
                  (ScreenUtil1.bottomBarHeight +
                      72 +
                      ScreenUtil1.statusBarHeight +
                      60),
            ),
            Column(
              children: [
                Container(
                  padding: EdgeInsets.only(top: 30.0.H),
                  height: 450.0.H,
                  child: MontageVideoListWidget(
                    info: "清空视频",
                    list: [],
                    h: 300.0.H,
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(top: 60.0.H),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      InkWell(
                        onTap: () {
                          inputOuputClick(inputOutputType.output);
                        },
                        child: Container(
                          height: 40.0.H,
                          width: 100.0.W,
                          decoration: BoxDecoration(
                            //设置边框
                            border:
                                Border.all(color: ColorPlate.white, width: 1),
                            //设置圆角
                            borderRadius: BorderRadius.circular((20.0.H)),
                          ),
                          child: Center(
                            child: Text(
                              "导出拼接配置",
                              textAlign: TextAlign.center,
                              style: StandardTextStyle.textWhite12Style,
                            ),
                          ),
                        ),
                      ),
                      Container(
                        width: 30.0.W,
                      ),
                      InkWell(
                        onTap: () {
                          inputOuputClick(inputOutputType.input);
                        },
                        child: Container(
                          height: 40.0.H,
                          width: 100.0.W,
                          decoration: BoxDecoration(
                            //设置边框
                            border:
                                Border.all(color: ColorPlate.white, width: 1),
                            //设置圆角
                            borderRadius: BorderRadius.circular((20.0.H)),
                          ),
                          child: Center(
                            child: Text(
                              "导入拼接配置",
                              textAlign: TextAlign.center,
                              style: StandardTextStyle.textWhite12Style,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            Positioned(
              bottom: 0,
              left: 20.0.W,
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(top: 10.0.H, bottom: 10.0.H),
                padding: EdgeInsets.only(left: 20.0.W, right: 20.0.W),
                height: 40.0.H,
                width: ScreenUtil1.screenWidth - 40.0.W,
                decoration: const BoxDecoration(
                  gradient: LinearGradient(
                      //渐变位置
                      begin: Alignment.topCenter, //右上
                      end: Alignment.bottomCenter, //左下
                      //渐变颜色[始点颜色, 结束颜色]
                      colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
                  borderRadius: BorderRadius.all(Radius.circular(8.0)),
                ),
                child: Row(
                  // mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      margin: EdgeInsets.only(right: 10.0.W),
                      width: 20.0.W,
                      height: 20.0.H,
                      child:
                          Image.asset(AssetUtils.getAsset2Image('icon_tip_w')),
                    ),
                    Text(
                      "提示搜索到的设备数和已配置的设备数",
                      style: StandardTextStyle.textWhite14Style,
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget iconTextBtn(
      int index, String title, String imgName, Function(int i) tapAction) {
    return InkWell(
      onTap: () {
        tapAction(index);
      },
      child: Container(
        width: 55.0.W,
        height: 60.0.H,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              width: 24.0.W,
              height: 24.0.W,
              child: Image.asset(AssetUtils.getAsset2Image(imgName)),
            ),
            Container(
              margin: const EdgeInsets.only(top: 5),
              child: Text(
                title,
                style: StandardTextStyle.textWhite10Style,
              ),
            )
          ],
        ),
      ),
    );
  }

  void inputOuputClick(inputOutputType type) {
    if (type == inputOutputType.input) {
      showDialog(
        barrierDismissible: true,
        context: context,
        builder: (context) {
          return DialogWidget(
            contentWidget: InputOutputDialog(
                onTapAction: () {
                  print("=======>1");
                },
                vc: nameController,
                btnName: "导入",
                type: type),
          );
        },
      );
    } else {
      showDialog(
        barrierDismissible: true,
        context: context,
        builder: (context) {
          return DialogWidget(
            contentWidget: InputOutputDialog(
                onTapAction: () {
                  print("=======>");
                },
                vc: nameController,
                btnName: "复制链接",
                type: type),
          );
        },
      );
    }
  }

  //按钮点击
  void btnClick(int index) {
    switch (index) {
      case 0:
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: const WifiSettingDialogMWidget(),
            );
          },
        );
        break;
      case 1:
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: const MBluetoothSettingDialogWidget(),
            );
          },
        );
        break;
      case 2:
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: const MAngleSettingDialogWidget(),
            );
          },
        );
        break;
      case 3:
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: const MPinjieManagerWidget(),
            );
          },
        );
        break;
      case 4:
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: const MClockDialogWidget(),
            );
          },
        );
        break;
      case 5:
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: const MPinjieDialogWidget(),
            );
          },
        );
        break;

      default:
    }
  }
}
